//
//  BDAccountManagerModel.m
//  BeidouMall
//
//  Created by qie on 16/3/31.
//  Copyright © 2016年 qie. All rights reserved.
//

#import "BDAccountManagerModel.h"

@implementation BDAccountManagerModel

+ (BDAccountManagerModel *)shareInstance {
    __strong static BDAccountManagerModel *sharedManager;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{ // 只会执行一次,且线程安全
        sharedManager = [[BDAccountManagerModel alloc] init];
    });
    
    return sharedManager;
}

#pragma mark 用户信息

// 用户 ID
- (NSString *)userId {
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"UserId"];
}

- (void)setUserId:(NSString *)userId {
    [[NSUserDefaults standardUserDefaults] setValue:userId forKey:@"UserId"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

// authorization
- (NSString *)authorization {
    NSString *auth = [[NSUserDefaults standardUserDefaults] stringForKey:@"Authorization"];
    return auth.length == 0 ? @"" : auth;// 避免传个nil
}

- (void)setAuthorization:(NSString *)authorization {
    [[NSUserDefaults standardUserDefaults] setValue:authorization forKey:@"Authorization"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSString *)tempAuthorization {
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"TempAuthorization"];
}

- (void)setTempAuthorization:(NSString *)tempAuthorization {
    [[NSUserDefaults standardUserDefaults] setValue:tempAuthorization forKey:@"TempAuthorization"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSString *)userName {
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"userName"];
}

- (void)setUserName:(NSString *)userName {
    if ([userName isEqual:[NSNull null]]) {
        userName = @"";
    }
    [[NSUserDefaults standardUserDefaults] setValue:userName forKey:@"userName"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSString *)phone {
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"Phone"];
}

- (void)setPhone:(NSString *)phone {
    if ([phone isEqual:[NSNull null]]) {
        phone = @"";
    }
    [[NSUserDefaults standardUserDefaults] setValue:phone forKey:@"Phone"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
-(NSString *)email {
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"Email"];
}

- (void)setEmail:(NSString *)email {
    if ([email isEqual:[NSNull null]]) {
        email = @"";
    }
    [[NSUserDefaults standardUserDefaults] setValue:email forKey:@"Email"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

// 密码
- (NSString *)password {
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"password"];
}

- (void)setPassword:(NSString *)password {
    [[NSUserDefaults standardUserDefaults] setValue:password forKey:@"password"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (double)latitude {
    return [[NSUserDefaults standardUserDefaults] doubleForKey:@"Latitude"];
}

- (void)setLatitude:(double)latitude {
    [[NSUserDefaults standardUserDefaults] setDouble:latitude forKey:@"Latitude"];
}

- (double)longitude {
    return [[NSUserDefaults standardUserDefaults] doubleForKey:@"Longitude"];
}

- (void)setLongitude:(double)longitude {
    [[NSUserDefaults standardUserDefaults] setDouble:longitude forKey:@"Longitude"];
}

- (NSInteger)cityCode {
    return [[NSUserDefaults standardUserDefaults] integerForKey:@"CityCode"];
}

- (void)setCityCode:(NSInteger)cityCode {
    [[NSUserDefaults standardUserDefaults] setInteger:cityCode forKey:@"CityCode"];
}

- (NSString *)adcode {
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"Adcode"];
}

- (void)setAdcode:(NSString *)adcode {
    [[NSUserDefaults standardUserDefaults] setValue:adcode forKey:@"Adcode"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSString *)inBoundary {
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"InBoundary"];
}

- (void)setInBoundary:(NSString *)inBoundary {
    [[NSUserDefaults standardUserDefaults] setValue:inBoundary forKey:@"InBoundary"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

// 是否登录
- (BOOL)isLogin {
    return [[NSUserDefaults standardUserDefaults] boolForKey:@"Login"];
}

- (void)setLogin:(BOOL)login {
    [[NSUserDefaults standardUserDefaults] setBool:login forKey:@"Login"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}


#pragma mark - Public method

// 保存用户信息
- (BOOL)saveNormarlUserInformation: (NSDictionary *) information {
    self.authorization = information[@"authorization"];
    self.userId = information[@"user"][@"userId"];
    self.phone = information[@"user"][@"telephone"];
    self.email = information[@"user"][@"email"];
    self.userName = information[@"user"][@"name"];
    return YES;
}

- (void)loginWithUrl:(NSString *)urlPath username:(NSString *)username password:(NSString *)password userType:(NSString *)userType completionHandler:(void (^)(NSError *))completionHandler {
    NSDictionary *parameters = @{@"account":username, @"password":password,@"type":userType};
    [[BDNetworking shareInstance] postWithUrl:urlPath sid:nil method:@"POST" parameters:parameters completionHandler:^(NSObject *results, NSError *error) {
        if (error != nil) {
            completionHandler(error);
        } else {
            if ([results isKindOfClass:[NSDictionary class]]) {
                NSDictionary *resultDic = (NSDictionary *)results;
                [self saveNormarlUserInformation:resultDic];
            }
            self.login = YES;
            completionHandler(nil);
        }
    }];
}

- (void)verifyLoginWithUrl:(NSString *) urlPath
                  username:(NSString *)username
                verifyCode:(NSString *)verifyCode
                  userType:(NSString *)userType
                      flag:(NSString *)flag
         completionHandler:(void (^)(NSDictionary *results, NSError *error))completionHandler {
    NSDictionary *parameters = @{@"account":username, @"verifyCode":verifyCode,@"type":userType};
    [[BDNetworking shareInstance] postWithUrl:urlPath sid:nil method:@"POST" parameters:parameters completionHandler:^(NSObject *results, NSError *error) {
        if (error != nil) {
            completionHandler(nil,error);
        } else {
            if ([flag isEqualToString:@"YES"]) {
                NSDictionary *resultDic = (NSDictionary *)results;
                self.tempAuthorization = resultDic[@"authorization"];
                completionHandler(resultDic,nil);
            } else {
                if ([results isKindOfClass:[NSDictionary class]]) {
                    NSDictionary *resultDic = (NSDictionary *)results;
                    [self saveNormarlUserInformation:resultDic];
                }
                self.login = YES;
                completionHandler(nil,nil);
            }
            
        }
    }];
}

- (void)logoutWithUrl:(NSString *)urlPath completionHandler:(void (^)(NSError *))completionHandler {
    [[BDNetworking shareInstance] postWithUrl:urlPath sid:nil method:@"PUT" parameters:nil completionHandler:^(NSObject *results, NSError *error) {
        if (error != nil) {
            completionHandler(error);
        } else {
            completionHandler(nil);
        }
    }];
}

- (void)checkUserWithUrl:(NSString *) urlPath
                   phone:(NSString *)phone
                userType:(NSString *)userType
       completionHandler:(void (^)(NSError *error))completionHandler {
    NSDictionary *parameters = @{@"account":phone, @"type":userType};
    [[BDNetworking shareInstance] postWithUrl:urlPath sid:nil method:@"PUT" parameters:parameters completionHandler:^(NSObject *results, NSError *error) {
        if (error != nil) {
            completionHandler(error);
        } else {
            if ([results isKindOfClass:[NSDictionary class]]) {
                NSDictionary *resultDic = (NSDictionary *)results;
                NSInteger exist = [resultDic[@"exist"] integerValue];
                if (exist == 0) {
                    completionHandler(nil);
                } else {
                    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"账号已存在"                                                                      forKey:NSLocalizedDescriptionKey];
                    NSError *cutsomError = [[NSError alloc] initWithDomain:@"aaa" code:998 userInfo:userInfo];
                    completionHandler(cutsomError);
                }
            }
        }
    }];
    
}

- (void)getVerifyCodeWithUrl:(NSString *) urlPath
           completionHandler:(void (^)(NSError *error))completionHandler {
    [[BDNetworking shareInstance] postWithUrl:urlPath sid:nil method:@"PUT" parameters:nil completionHandler:^(NSObject *results, NSError *error) {
        if (error != nil) {
            completionHandler(error);
        } else {
            completionHandler(nil);
        }
    }];
}

- (void)registerWithWithUrl:(NSString *) urlPath
                   username:(NSString *)username
                      phone:(NSString *)phone
                   password:(NSString *)password
                 verifyCode:(NSString *)verifyCode
                   userType:(NSString *)userType
          completionHandler:(void (^)(NSError *error))completionHandler {
    
    NSDictionary *parameters = @{@"account":phone,
                                 @"verifyCode":verifyCode,
                                 @"password":password,
                                 @"name":username,
                                 @"type":userType};
    
    [[BDNetworking shareInstance] postWithUrl:urlPath sid:nil method:@"POST" parameters:parameters completionHandler:^(NSObject *results, NSError *error) {
        if (error != nil) {
            completionHandler(error);
        } else {
            if ([results isKindOfClass:[NSDictionary class]]) {
                NSDictionary *resultDic = (NSDictionary *)results;
                [self saveNormarlUserInformation:resultDic];
            }
            self.login = YES;
            completionHandler(nil);
        }
    }];
}

- (void)modifyPwdWIthUrl:(NSString *)urlPath
                  userId:(NSString *) userId
                password:(NSString *) password
       completionHandler:(void (^)(NSError *error)) completionHandler {
    NSDictionary *parameters = @{@"userId":userId,
                                 @"password":password};
    
    [[BDNetworking shareInstance] postWithUrl:urlPath sid:nil method:@"POST" parameters:parameters completionHandler:^(NSObject *results, NSError *error) {
        if (error != nil) {
            completionHandler(error);
        } else {
            completionHandler(nil);
        }
    }];
}

@end
